package com.example.myapplication;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class FriendAdapter extends RecyclerView.Adapter<FriendAdapter.ViewHolder> {
    private List<Friend> friendList;

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 创建ViewHolder
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.friend_item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.name.setText(String.valueOf(friendList.get(position).getName()));
        holder.description.setText(friendList.get(position).getDescription());
        // 对子项数据进行赋值
        Friend friend = friendList.get(position);
        holder.avatorImg.setImageResource(friend.getImgId());
        holder.description.setText(friend.getDescription());
    }

    @Override
    public int getItemCount() {
        // 返回RecyclerView的子项数目
        return friendList.size();

    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView avatorImg;
        TextView description;
        TextView name;

        public ViewHolder (View view)
        {
            super(view);
            avatorImg =  view.findViewById(R.id.avator);
            name =  view.findViewById(R.id.name);
            description =  view.findViewById(R.id.description);
        }

    }
    public  FriendAdapter (List <Friend> friendList){
        this.friendList = friendList;
    }
}
